/*
 * Packet.cpp
 *
 *  Created on: Apr 14, 2011
 *      Author: Alan Mattila
 *
 *  The Packet class defines the structure of the packet
 *  that will be sent over the network.
 */

#include <iostream>
#include <iomanip>
#include <cstdint>
#include <cassert>

#include "Packet.h"
#include "../misc/debug.h"


Packet::Packet()
{
	//
}


Packet::~Packet()
{
	//
}


/*********************************************************
 * packetType_t Packet::GetPacketType()
 ********************************************************/
packetType_t Packet::GetPacketType(void) const
{
	return (packetType_t) packetData.type;
}


/*********************************************************
 * packetMessage_t Packet::GetPacketMessage()
 ********************************************************/
packetMessage_t Packet::GetPacketMessage(void) const
{
	return (packetMessage_t) packetData.message;
}


/*********************************************************
 * void Packet::GetPacketGameBoard(
 ********************************************************/
void Packet::GetPacketGameBoard(Board *board) const
{
	for(int i=0; i<BOARD_Y; i++)
	{
		for(int j=0; j<BOARD_X; j++)
		{
			board->SetValue(j, i, (int) packetData.gameBoard[i*BOARD_Y +j]);
		}
	}
}


/*********************************************************
 * int Packet::GetPacketStatus()
 ********************************************************/
int Packet::GetPacketStatus(void) const
{
	return (int) packetData.status;
}


/*********************************************************
 * int Packet::GetPacketX1()
 ********************************************************/
int Packet::GetPacketX1(void) const
{
	return (int) packetData.x1;
}


/*********************************************************
 * int Packet::GetPacketY1()
 ********************************************************/
int Packet::GetPacketY1(void) const
{
	return (int) packetData.y1;
}


/*********************************************************
 * int Packet::GetPacketX2()
 ********************************************************/
int Packet::GetPacketX2(void) const
{
	return (int) packetData.x2;
}


/*********************************************************
 * int Packet::GetPacketY2()
 ********************************************************/
int Packet::GetPacketY2(void) const
{
	return (int) packetData.y2;
}


/*********************************************************
 * void Packet::SetPacketType()
 ********************************************************/
void Packet::SetPacketType(packetType_t packetType)
{
	packetData.type = (uint8_t) packetType;
}


/*********************************************************
 * void Packet::SetPacketMessage()
 ********************************************************/
void Packet::SetPacketMessage(packetMessage_t packetMessage)
{
	packetData.message = (uint8_t) packetMessage;
}


/*********************************************************
 * void Packet::SetPacketGameBoard()
 ********************************************************/
void Packet::SetPacketGameBoard(Board *board)
{
	for(int i=0; i<BOARD_Y; i++)
	{
		for(int j=0; j<BOARD_X; j++)
		{
			packetData.gameBoard[i*BOARD_Y +j] = (uint8_t) board->GetValue(j, i);
		}
	}
}


/*********************************************************
 * void Packet::SetPacketStatus()
 ********************************************************/
void Packet::SetPacketStatus(int packetStatus)
{
	packetData.status = (uint8_t) packetStatus;
}


/*********************************************************
 * void Packet::SetPacketMove()
 ********************************************************/
void Packet::SetPacketMove(int x1, int y1, int x2, int y2)
{
	packetData.x1 = (uint8_t) x1;
	packetData.y1 = (uint8_t) y1;
	packetData.x2 = (uint8_t) x2;
	packetData.y2 = (uint8_t) y2;
}


/*********************************************************
 * int Packet::GetPacketSize()
 ********************************************************/
int Packet::GetPacketSize(void) const
{
	if(packetData.type == (uint8_t) Message)
		return MESSAGE_PACKET_SIZE;
	else if(packetData.type == (uint8_t) GameData)
		return GAME_DATA_PACKET_SIZE;
	else if(packetData.type == (uint8_t) Move)
		return MOVE_PACKET_SIZE;
	else
		return 0;
}


/*********************************************************
 * uint8_t Packet::GetByte()
 ********************************************************/
uint8_t Packet::GetByte(int index) const
{
	assert(index < MAX_PACKET_SIZE);
	return data[index];
}


/*********************************************************
 * uint8_t Packet::SetByte()
 ********************************************************/
void Packet::SetByte(uint8_t byte, int index)
{
	assert(index < MAX_PACKET_SIZE);
	data[index] = byte;
}


/*
 * Debug: print contents of packet to screen
 */
void Packet::DisplayContents(void)
{
	TRACE("Displaying contents of packet:\n");
	int size = this->GetPacketSize();
	for(int i=0; i<size; i++)
	{
		std::cout << std::setw(2) << i << ": " << (int)data[i] << std::endl;
	}
}
